/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.psa.persistence;

import br.edu.psa.beans.exceptions.NonexistentEntityException;
import br.edu.psa.beans.exceptions.RollbackFailureException;
import br.edu.psa.model.Tema;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author JOAO e ANDRIGO
 */
public class TemaJpaController extends Controller implements Serializable {

    public TemaJpaController(EntityManager em) {
        this.setEntityManager(em);
    }

    public void create(Tema tema) throws RollbackFailureException, Exception {
        try {
            em.persist(tema);
        } catch (Exception ex) {
            throw ex;
        } 
    }

    public void edit(Tema tema) throws NonexistentEntityException, RollbackFailureException, Exception {
        try {
            tema = em.merge(tema);
        } catch (Exception ex) {

            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = tema.getId();
                if (findTema(id) == null) {
                    throw new NonexistentEntityException("The tema with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } 
    }

    public void destroy(Long id) throws NonexistentEntityException, RollbackFailureException, Exception {
        try {

            Tema tema;
            try {
                tema = em.getReference(Tema.class, id);
                tema.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tema with id " + id + " no longer exists.", enfe);
            }
            em.remove(tema);

        } catch (Exception ex) {

            throw ex;
        } 
    }
    
    public Tema findTemaByName(String name){
        StringBuilder sql = new StringBuilder();
        sql.append("select t from Tema t where t.nome = :nome");
        Query query = em.createQuery(sql.toString());
        query.setParameter("nome", name);
        Tema t = (Tema) query.getSingleResult();
        return t;
    }

    public List<Tema> findTemaEntities() {
        return findTemaEntities(true, -1, -1);
    }

    public List<Tema> findTemaEntities(int maxResults, int firstResult) {
        return findTemaEntities(false, maxResults, firstResult);
    }

    private List<Tema> findTemaEntities(boolean all, int maxResults, int firstResult) {
       
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Tema.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
       
    }

    public Tema findTema(Long id) {      
        return em.find(Tema.class, id);        
    }

    public int getTemaCount() {       
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Tema> rt = cq.from(Tema.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();        
    }
}
